<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>社区类型</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="${resRoot}/layui/css/layui.css" media="all" />
  <link rel="stylesheet" href="${resRoot}/layui/admin/admin.css" media="all" />
	<link id="layuicss-layer" rel="stylesheet" href="${resRoot}/layui/css/modules/layer/default/layer.css" media="all">
</head>
<body layadmin-themealias="default">
          <div class="layui-fluid">   
          <div class="layui-card">
          <div class="layui-card-body">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend><#if CommunityTypeDTO??>更新<#else>新增</#if>社区类型</legend>
</fieldset>
 
<form class="layui-form" action="#" >
	<input type="hidden" id="id" value="<#if CommunityTypeDTO??>${CommunityTypeDTO.id}<#else>0</#if>" >
	
  <div class="layui-form-item">
    <label class="layui-form-label">所属类型</label>
    <div class="layui-input-inline">
      <select name="type" id="type" lay-verify="required" >
        <option value=""></option>
        <#list communityTypeMap?keys as key>
        <option value="${key}" <#if CommunityTypeDTO?? && key==CommunityTypeDTO.type >selected="selected"</#if> >${communityTypeMap[key]}</option>
        </#list>
      </select>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">类型名称</label>
    <div class="layui-input-inline">
      <input type="text" name="name" id="name" value="<#if CommunityTypeDTO??>${CommunityTypeDTO.name}</#if>" lay-verify="required" autocomplete="off" placeholder="请输入类型名称" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">排序</label>
    <div class="layui-input-inline">
      <input type="number" id="sort" value="<#if sort??>${sort}</#if>" name="lable" lay-verify="required" placeholder="请输入排序" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">简介</label>
    <div class="layui-input-block">
	  <textarea name="desc" id="detial" class="layui-textarea"><#if CommunityTypeDTO??>${CommunityTypeDTO.detial!}</#if></textarea>
    </div>
  </div>
  
  
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">上传图标</label>
      <div class="layui-input-inline">
      <div class="site-demo-upload">
 <!--  <img id="LAY_demo_upload" src="<#if CommunityTypeDTO??>${CommunityTypeDTO.iconUrl}</#if>" style="max-width:200px;max-height:200px;"> -->
    <img id="LAY_demo_upload" imgName="<#if CommunityTypeDTO??>${CommunityTypeDTO.iconUrl}</#if>" src="<#if CommunityTypeDTO?? && CommunityTypeDTO.iconUrl?? >${picUrlHost}${CommunityTypeDTO.iconUrl!}</#if>" style="max-width:200px;max-height:200px;">
  
  
  <button type="button" class="layui-btn" id="test">
  <i class="layui-icon"></i>上传图标
</button><input class="layui-upload-file" type="file" accept="images" name="fileField" id="fileField">
</div>
      </div>
    </div>
  </div>
  <input type="hidden" id="iconUrl" name="iconUrl" value="<#if CommunityTypeDTO??>${CommunityTypeDTO.iconUrl}</#if>" >

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" id="submitBtn"  lay-submit=""  type="button">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      <button type="button" class="layui-btn layui-btn-primary" onclick="history.go(-1);" >返回</button>
    </div>
  </div>
</form>
 </div>
          </div>
          </div>
<script src="${resRoot}/layui/layui.js" charset="utf-8"></script>
<script src="${resRoot}/js/jquery.min.js?v=2.1.4"></script>
<script type="text/javascript" src="${resRoot}/js/ajaxfileupload.js"></script>
<script>
layui.use(['form', 'layedit', 'laydate'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate;
  
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 5){
        return '用户名必须不小于5';
      }
    }
    ,pass: [/(.+){6,12}$/, '密码必须6到12位']
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  
  $("#submitBtn").click(function(){
		var id = $("#id").val();
		  var iconUrl=$("#LAY_demo_upload").attr("imgName");
				var name =$("#name").val();
				var type =$("#type").val();
				var iconUrl = $("#iconUrl").val();
				var sort=$("#sort").val();
				var detial=$("#detial").val();
				var flag = true;
				if(name==""|| name== null || name=="undifined") {
					layer.msg("请输入类型名称！");
					flag = false;
					return false;
				}
				if(type==""|| type== null || type=="undifined") {
					layer.msg("请选择所属类型！");
					flag = false;
					return false;
				}
				if(iconUrl==""|| iconUrl== null || iconUrl=="undifined") {
					layer.msg("请上传图标！");
					flag = false;
					return false;
				}
				if(sort==""|| sort == null || sort=="undifined") {
					layer.msg("请输入排序");
					flag = false;
					return false;
				}
				if(detial==""|| detial == null || detial=="undifined") {
					layer.msg("请输入简介");
					flag = false;
					return false;
				}
				if(flag) {
					$.ajax({
						type : "post",
						url : "${base}/sys/communityType/save",
						data : {
							id : id,
							name : name,
							type : type,
							iconUrl : iconUrl,
							detial : detial,
							sort : sort
							},
						dataType : "json",
						async : false,
						success : function(data) {
							if(data.message == 2) {
								layer.alert('请完善数据');   
									// return false;
								} else if(data.message == 1) {
									layer.msg('保存成功！', {
									  icon: 1,
									  time: 2000 //2秒关闭（如果不配置，默认是3秒）
									}, function(){
										window.location.href="${base}/sys/communityType/show";
									});   
								} else {
									layer.msg('系统繁忙！'); 
								}
						}
					}); 
				}
	});
  
  layui.use('upload', function(){
	  var upload = layui.upload;
		var uploadInst = upload.render({
		    elem: '#test' //绑定元素
		    ,url: '${base}/uploadImage' //上传接口
		    ,done: function(res){
		    if(res != "") {
	    		var obj = res;
				$("#iconUrl").val(obj.imageUrl);
				$("#LAY_demo_upload").attr("src",'${picUrlHost}' +obj.imageUrl);
	    		
			}
		      //上传完毕回调
		    }
		    ,error: function(){
		      //请求异常回调
		    }
		  });

	  
 	});  

	});  
</script>
</body>
</html>
